﻿using System;
using SdmxMl.Common;
using SdmxMl.Manager;
using SdmxMl.Structure;
using SdmxMl.GenericMetadata;
using Telerik.WinControls.UI;

namespace PluginMeb
{
    public class FillMetaset
    {
        private MetadataSetType metadataSet;
        private MetadataStructureDefinitionType msd;
        private SmManager sm;

        public MetadataSetType PersistGridToSdmx(int cantonIndex, RadGridView gridView, string agencyId)
        {
            sm = new SmManager();

            metadataSet = ImportMetasetFromExcel.CreateMetaset(cantonIndex, agencyId);

            // Fetch MSD and report for matrix
            msd = sm.GetArtefact(SdmxArtefactType.MetadataStructureDefinitions,
                metadataSet.MetadataStructureRef, true) as MetadataStructureDefinitionType;
            ReportStructureType report = msd.ReportStructureList[0];

            foreach (GridViewRowInfo row in  gridView.MasterTemplate.Rows)
            {
                AttributeValueSetType atv = new AttributeValueSetType(msd, report, metadataSet);

                // The target values
                atv.TargetValuesTypeList.SetComponentValue(ImportMetasetFromExcel.KEY_BILDUNG_CH, 
                    MsValue(row.Cells[GridCreator.COL_BJAHR_CH].Value));
                atv.TargetValuesTypeList.SetComponentValue(ImportMetasetFromExcel.KEY_ALTER_MIN,
                    MsValue(row.Cells[GridCreator.COL_AGE_INF].Value));
                atv.TargetValuesTypeList.SetComponentValue(ImportMetasetFromExcel.KEY_ALTER_MAX,
                    MsValue(row.Cells[GridCreator.COL_AGE_SUP].Value));
                atv.TargetValuesTypeList.SetComponentValue(ImportMetasetFromExcel.KEY_GULT_AB,
                    MsValue(row.Cells[GridCreator.COL_ES_VF].Value));
                atv.TargetValuesTypeList.SetComponentValue(ImportMetasetFromExcel.KEY_GULT_BIS,
                    MsValue(row.Cells[GridCreator.COL_ES_VT].Value));
                atv.TargetValuesTypeList.SetComponentValue(ImportMetasetFromExcel.KEY_KLASSE_CH,
                    MsValue(row.Cells[GridCreator.COL_KLASSE_CH].Value));
                atv.TargetValuesTypeList.SetComponentValue(ImportMetasetFromExcel.KEY_BILDUNG_KT,
                    MsValue(row.Cells[GridCreator.COL_BJAHR_KT].Value));
                atv.TargetValuesTypeList.SetComponentValue(ImportMetasetFromExcel.KEY_SCHULART,
                    MsValue(row.Cells[GridCreator.COL_SCHULART].Value));


                // The reported attributes (no comment at creation)
                SetRepAttValue(atv, ImportMetasetFromExcel.REPORT_COMMENT, row.Cells[GridCreator.COL_COMMENT].Value);

                metadataSet.AttributeValueSetList.Add(atv);

            }

            return metadataSet;
        }

        private string MsValue(object val)
        {
            string rep = val == null ? string.Empty : val.ToString();
            return rep.Length == 0 ? ImportMetasetFromExcel.EMPTY_MARKER : rep;
        }

        private void SetRepAttValue(AttributeValueSetType at, string conceptId, object val)
        {
            ReportedAttributeType ra = at.ReportedAttributeList.GetReportedAttributeOfConcept(conceptId);
                if (ra != null)
                {
                    ra.Value = val == null || val.ToString() == string.Empty ? new MultiLang(ImportMetasetFromExcel.EMPTY_MARKER) : new MultiLang(val.ToString());
                }
        }
    
    }
}
